package com.qf.conf;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    //声明交换机
    @Bean
    public Exchange bootEx(){
        return new ExchangeBuilder("bootEx", ExchangeTypes.TOPIC).build();
    }

    //声明队列
    @Bean
    public Queue bootQue1(){
        return QueueBuilder.durable("bootQue1").build();
    }
    @Bean
    public Queue bootQue2(){
        return QueueBuilder.durable("bootQue2").build();
    }

    //声明绑定关系
    @Bean
    public Binding que1Binding(Queue bootQue1, Exchange bootEx){
        return BindingBuilder.bind(bootQue1).to(bootEx).with("abc.*").noargs();
    }
    @Bean
    public Binding que2Binding(Queue bootQue2, Exchange bootEx){
        return BindingBuilder.bind(bootQue2).to(bootEx).with("abc.#").noargs();
    }

    //声明一个支持持久化的交换机
    @Bean
    public Exchange durableEx(){
        return ExchangeBuilder.topicExchange("durableEx").durable(true).build();
    }
    //声明一个支持持久化的队列
    @Bean
    public Queue durableQue(){
        return QueueBuilder.durable("durableQue").build();
    }
    //绑定关系
    @Bean
    public Binding durableQueBinding(Queue durableQue, Exchange durableEx){
        return BindingBuilder.bind(durableQue).to(durableEx).with("durable.#").noargs();
    }

    /**===============死信===================**/
    //演示死信的普通交换机
    @Bean
    public Exchange dead_common_ex(){
        return ExchangeBuilder.topicExchange("dead_common_ex").durable(true).build();
    }
    @Bean
    public Queue dead_common_que(){
        return QueueBuilder
                .durable("dead_common_que")
                //当当前队列中有消息成为死信后，会将消息转到该方法指定的交换机
                .deadLetterExchange("dead_ex")
                //当指定的死信交换机收到消息后，通过这个方法设定的路由键，将消息转发到对应的队列中
                .deadLetterRoutingKey("dead.letter.abc")
                //设置队列的消息最大个数(运行前，要将之前的同名队列删除)
                .maxLength(5)
                //设置队列中消息的有效时间
                .ttl(5000)
                .build();
    }
    @Bean
    public Binding binding(Exchange dead_common_ex,Queue dead_common_que){
        return BindingBuilder.bind(dead_common_que).to(dead_common_ex).with("dead.common.#").noargs();
    }

    //声明死信交换机和死信队列
    @Bean
    public Exchange dead_ex(){
        return ExchangeBuilder.topicExchange("dead_ex").durable(true).build();
    }
    @Bean
    public Queue dead_que(){
        return QueueBuilder.durable("dead_que").build();
    }
    @Bean
    public Binding dead_binding(Exchange dead_ex,Queue dead_que){
        return BindingBuilder.bind(dead_que).to(dead_ex).with("dead.letter.#").noargs();
    }

    /**=============惰性队列=============**/
    @Bean
    public Queue lazy_que(){
        return QueueBuilder
                .durable("lazy_que")
                .lazy()  //声明该队列为惰性队列（消息会保存到磁盘，消费时，从磁盘加载回内存）
                .build();
    }
    @Bean
    public Exchange lazy_ex(){
        return ExchangeBuilder.directExchange("lazy_ex").durable(true).build();
    }
    @Bean
    public Binding lazy_binding(Exchange lazy_ex,Queue lazy_que){
        return BindingBuilder.bind(lazy_que).to(lazy_ex).with("lazy").noargs();
    }

    /**=============幂等队列=============**/
    @Bean
    public Queue mideng_que(){
        return QueueBuilder
                .durable("mideng_que")
                .build();
    }
    @Bean
    public Exchange mideng_ex(){
        return ExchangeBuilder.directExchange("mideng_ex").durable(true).build();
    }
    @Bean
    public Binding mideng_binding(Exchange mideng_ex,Queue mideng_que){
        return BindingBuilder.bind(mideng_que).to(mideng_ex).with("mideng").noargs();
    }
}
